﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace PageAnalytic.Web
{
    /// <summary>
    /// Summary description for PageHandler
    /// </summary>
    public class PageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }



        #region CacheItems Class
        /// <summary>
        /// Main Cache Item Class
        /// </summary>
        internal class CacheItem
        {
            public string name
            {
                get;
                set;
            }

            public string title
            {
                get;
                set;
            }

            public string lang
            {
                get;
                set;
            }

            public string key
            {
                get;
                set;
            }

            public ContentCache cache
            {
                get;
                set;
            }

            public CacheItem(string name, int expire, bool isDynamic, string lang)
            {
                this.name = name.ToLowerInvariant();
                this.cache = new ContentCache(expire, isDynamic);
                this.lang = lang.ToLowerInvariant();
                this.key = name + "-" + lang;
            }
        }

        /// <summary>
        /// Contains cached content objects
        /// </summary>
        internal class CacheItems : CollectionBase
        {
            public CacheItem this[int index]
            {
                get
                {
                    return (CacheItem)this.List[index];
                }
            }

            public CacheItem this[object name]
            {
                get
                {
                    CacheItem citem = null;
                    foreach (CacheItem c in this.List)
                    {
                        if (c.name == (string)name)
                            citem = c;
                    }
                    return citem;
                }
            }

            public CacheItem this[string key]
            {
                get
                {
                    CacheItem citem = null;
                    foreach (CacheItem c in this.List)
                    {
                        if (c.key == key)
                            citem = c;
                    }
                    return citem;
                }
            }

            public void Add(CacheItem item)
            {
                base.List.Add(item);
            }
        }

        /// <summary>
        /// Contains cached items with content, expire time, cache status etc.
        /// </summary>
        internal class ContentCache
        {
            /// <summary>
            /// String, Cached content (html usually)
            /// </summary>
            internal string Content = "";
            /// <summary>
            /// String, contains untouched raw html content
            /// </summary>
            internal string RawContent = "";
            /// <summary>
            /// Int, Expire time in minutes
            /// </summary>
            public int ExpireMinute;
            /// <summary>
            /// Bool, Dynamic Content Indicator
            /// </summary>
            public bool IsDynamic;
            /// <summary>
            /// DateTime, Exact expire time of content
            /// </summary>
            internal DateTime Expire = DateTime.Now;
            /// <summary>
            /// DateTime, Process start date for ongoing cache reload processes
            /// </summary>
            internal DateTime ProcessStart = DateTime.Now;
            /// <summary>
            /// CacheStatus, status of cache reload process
            /// </summary>
            internal CacheStatus CacheStat = CacheStatus.New;
            /// <summary>
            /// Cache time
            /// </summary>
            internal Double CacheTime = 0;

            /// <summary>
            /// Insctructor
            /// </summary>
            /// <param name="expireMinute">Expire time in minute</param>
            public ContentCache(int expireMinute, bool isDynamic)
            {
                ExpireMinute = expireMinute;
                IsDynamic = isDynamic;
            }


        }

        /// <summary>
        /// Enum, Cache Statuses
        /// </summary>
        public enum CacheStatus
        {
            New,
            Started,
            Cached
        }

        #endregion

    }
}